<template>
  <div class="detail-container">
    <van-nav-bar
      title="商品详情"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
    >
      <template #right>
        <van-icon name="search" size="22" />
      </template>
    </van-nav-bar>
    <van-swipe :autoplay="3000" v-if="gallery">
      <van-swipe-item v-for="item in gallery" :key="item.id">
        <img v-lazy="item.img_url" />
      </van-swipe-item>
    </van-swipe>
    <compTips></compTips>
    <div class="goods">
      <div class="goods_info" v-if="info">
        <h2 class="name">{{ info.name }}</h2>
        <div class="brief">{{ info.goods_brief }}</div>
        <div class="price" style="color: darkred">
          {{ info.retail_price | filterMoney }}
        </div>
      </div>
      <div class="attribute" v-if="attribute">
        <h3>商品参数</h3>
        <ul>
          <li v-for="(item, index) in attribute" :key="index">
            <div class="l">{{ item.name }}</div>
            <div class="r van-ellipsis">{{ item.value }}</div>
          </li>
        </ul>
      </div>
      <div class="goods_desc">
        <h2>商品详细介绍</h2>
        <div class="" v-html="goods_desc"></div>
      </div>
      <div class="issue">
        <h2><span>常见问题</span></h2>
        <div v-for="(item, index) in issue" :key="index" class="item">
          <div class="l"><van-icon name="question" /></div>
          <div class="r">
            <h4 class="question">{{ item.question }}</h4>
            <div class="answer">{{ item.answer }}</div>
          </div>
        </div>
      </div>
      <!-- 相关产品 -->
      <div class="related">
        <h2><span>大家都在看</span></h2>
        <product :goodsList="goodsList"></product>
      </div>
      <!-- sku商品规格 -->
      <van-sku
        ref="sku"
        v-model="isSkuShow"
        :sku="sku"
        :goods="goods"
        :quota="quota"
        :hide-stock="sku.hide_stock"
      >
      </van-sku>
      <!-- 商品导航 -->
      <GoodsAction @gotoCart="gotoCart" :goodsCount="goodsCount"></GoodsAction>
    </div>
  </div>
</template>

<script>
import {
  getGoodsDetailData,
  getRelatedData,
  getGoodsCount,
  addToCart,
} from "@/request/api";
import product from "@/components/product";
import compTips from "@/components/compTips";
import GoodsAction from "@/components/GoodsAction";
export default {
  components: {
    compTips,
    product,
    GoodsAction,
  },
  data() {
    return {
      //轮播图
      gallery: [],
      //商品信息
      info: null,
      //商品参数
      attribute: [],
      //商品详细介绍
      goods_desc: "",
      //常见问题
      issue: [],
      //相关产品
      goodsList: [],
      //购物车产品数量
      goodsCount: 0,
      productList: [],
      //是否显示商品规格,false隐藏
      isSkuShow: false,
      quota: 100,
      sku: {
        tree: [],
        hide_stock: false,
        price: 0,
        stock_num: 0,
      },
      goods: {
        picture: "https://img01.yzcdn.cn/1.jpg",
      },
    };
  },
  methods: {
    onClickLeft() {
      this.$router.go(-1);
    },
    onClickRight() {
      this.$router.push("/home/popup");
    },

    gotoCart() {
      if (this.isSkuShow) {
        //加入购物车
        addToCart({
          goodsId: this.$route.query.gid,
          productId: this.productList[0].id,
          number: this.$refs.sku.selectedNum,
        })
          .then((res) => {
            if (res.errno == 0) {
              let {
                cartTotal: { goodsCount },
              } = res.data;
              this.goodsCount = goodsCount;
              this.$toast("加入购物车成功");
              this.isSkuShow = false;
            } else if (res.errno == 400) {
              this.$toast("库存不足");
            }
          })
          .catch((err) => {
            this.$toast(err);
          });
      } else {
        this.isSkuShow = true;
      }
    },
  },
  activated() {
    getGoodsDetailData({
      id: this.$route.query.gid,
    }).then((res) => {
      if (res.errno == 0) {
        let { gallery, info, attribute, issue, productList } = res.data;
        this.gallery = gallery;
        this.info = info;
        this.attribute = attribute;
        this.goods_desc = info.goods_desc;
        this.issue = issue;
        this.goods.picture = info.list_pic_url;
        this.sku.price = info.retail_price;
        this.sku.stock_num = info.goods_number;
        this.productList = productList;
      }
    });
    getRelatedData({
      id: this.$route.query.gid,
    }).then((res) => {
      if (res.errno == 0) {
        let { goodsList } = res.data;
        this.goodsList = goodsList;
      }
    });
  },
  created() {
    //获取购物车商品数量
    getGoodsCount().then((res) => {
      if (res.errno == 0) {
        this.goodsCount = res.data.cartTotal.goodsCount;
      }
    });
  },
};
</script>

<style lang="less" scoped>
.detail-container {
  padding-bottom: 55px;
}
.van-swipe {
  img {
    width: 100%;
    height: 280px;
  }
}
.goods {
  .goods_info {
    text-align: center;
    background-color: #fff;
    padding: 20px;
    line-height: 30px;
    margin-bottom: 10px;
    div {
      color: #333;
    }
    .name {
      font-size: 20px;
      color: #000;
    }
    .brief {
      font-size: 16px;
    }
    .price {
      font-size: 16px;
      color: darkred;
    }
  }
  .attribute {
    padding: 10px;
    background-color: #fff;
    margin-bottom: 10px;
    ul {
      margin: 10px 0;
      li {
        display: flex;
        line-height: 40px;
        background-color: #efefef;
        margin-bottom: 5px;
        .l {
          width: 30%;
          text-align: right;
          margin-right: 10px;
          font-size: 14px;
          color: #999;
        }
        .r {
          flex: 1;
          font-size: 18px;
        }
      }
    }
  }
  /deep/ .goods_desc {
    padding: 10px;
    background-color: #fff;
    h2 {
      line-height: 30px;
    }
    img {
      width: 100%;
    }
  }
  .issue,
  .related {
    padding: 10px;
    background-color: #fff;
    margin-top: 20px;
    h2 {
      width: 180px;
      height: 4px;
      margin: 10px auto 30px;
      text-align: center;
      background-color: #fff;
      border-bottom: 1px solid #333;
      position: relative;
      span {
        display: block;
        width: 90px;
        background-color: red;
        padding: 5px;
        position: absolute;
        top: -8px;
        left: 50%;
        transform: translateX(-50%);
        background-color: #fff;
        font-size: 18px;
      }
    }
    .item {
      display: flex;
      line-height: 20px;
      .l {
        margin-right: 10px;
        color: darkred;
        font-size: 20px;
      }
      .r {
        .question {
          line-height: 24px;
        }
        .answer {
          font-size: 14px;
          color: #777;
        }
      }
    }
  }
}
</style>